home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / manual-p / vh-man2h.000 / vh-man2h / vh-man2html-1.3 / mansearch < prev    next >
Text File  |  1996-04-05  |  4KB  |  149 lines

  1. #!/bin/sh
  2. #
  3. #  Interface to a glimpse search of the man pages.
  4. #  Michael Hamilton <michael@actrix.gen.nz>
  5. #
  6. QUOTE="'"
  7. export QUOTE
  8.  
  9. exec awk '
  10. function removeopts(string) {
  11.   gsub(/^[ \t]/, "", string);    # Remove leading spaces
  12.   gsub(/[ \t]$/, "", string);    # Remove trailing spaces
  13.   gsub(/[ \t\\];/, ";", string);    # Remove spaces before ;
  14.   gsub(/[ \t];/, ",", string);  # Remove spaces before ,
  15.   # print "---" string "---" > "/dev/stderr"
  16.   while (match(string, /^-[FLBwk1-8]/)) {
  17.     if (match(string, /^-[FL]( |.)[^ \t]+[ \t]+/)) { # Option with arg
  18.       options = options " " substr(string, RSTART, RLENGTH);
  19.       string = substr(string, RSTART + RLENGTH);
  20.     } 
  21.     else if (match(string, /^-[Bwk1-8][ \t]+/)) { # Option without arg
  22.       options = options " " substr(string, RSTART, RLENGTH);
  23.       string = substr(string, RSTART + RLENGTH);
  24.     }
  25.     else if (match(string, /^-[^ \t]/)) { # Remove it
  26.       string = substr(string, RSTART + RLENGTH);   
  27.     }
  28.   }
  29.   return string;
  30. }
  31.  
  32. BEGIN {
  33.  
  34.   quote = ENVIRON["QUOTE"];
  35.   truncate_at = 11;        # Single page display match limit.
  36.  
  37.   glimpse_cmd = "glimpse -H /var/man2html -y -W -i "
  38.  
  39.   for (i = 1; i < ARGC; i++) {
  40.     # print "---" i "---" ARGV[i] "---" > "/dev/stderr"
  41.     string = string " " ARGV[i];
  42.   }
  43.                 # Have to be carefull to single quote this
  44.                 # string later.
  45.   string = removeopts(string);
  46.  
  47.   gsub(/[^a-zA-Z0-9-_+ \t\/@%;,$*|]/, " ", string);
  48.  
  49.   searchdocument = "/home/httpd/html/mansearch.html";
  50.   if (!string) {
  51.     while (getline line < searchdocument) {
  52.       print line;
  53.     }
  54.     exit;
  55.   }
  56.  
  57.   print "Content-type: text/html";
  58.   print "";
  59.   print "<HTML>";
  60.   print "<HEAD>";
  61.   print "<TITLE>Manual Pages - Search Results: " string "</TITLE>";
  62.   print "</HEAD>";
  63.   print "<BODY>";
  64.   
  65.   print "<H1>Manual Pages - Search Results</H1>";
  66.   print "<H2>Target text: " options " " string "</H2>";
  67.  
  68.   print "<A HREF=\"http:/cgi-bin/mansearch\">";
  69.   print "Perform another search";
  70.   print "</A><BR>";
  71.   print "<A HREF=\"http:/cgi-bin/man2html\">";
  72.   print "Return to Main Contents";
  73.   print "</A>";
  74.  
  75.   print "<HR>";  
  76.  
  77.   print "<DL>";
  78.                 # Unless you like being hacked, the single
  79.                 # forward quotes are most important.
  80.   cmd = glimpse_cmd " " options " " quote string quote " 2>&1" ;
  81.   #print cmd > "/dev/stderr"
  82.  
  83.   while (cmd | getline matchline) {
  84.     if (split(matchline, part, ":") == 1) {
  85.       continue;
  86.     }
  87.     else {
  88.       filename = part[1];
  89.     }
  90.  
  91.     if (filename == "glimpse") {
  92.       print "<DT><B>"filename"</B>:";
  93.     }
  94.     else if (filename != last_filename) {
  95.       mcount++;
  96.       tcount = 0;
  97.       last_filename = filename ;
  98.       last_text = "";
  99.       match(filename, ".*/");
  100.       if (match(filename, ".*/")) {
  101.     dirname = substr(filename, 1, RLENGTH);
  102.     filename = substr(filename, RLENGTH + 1);
  103.     if (dirname != last_dirname) {
  104.       last_dirname = dirname;
  105.           print "</DL>";
  106.       print "<H3>Location: " dirname "</H3>";
  107.       print "<DL>";
  108.     }
  109.       }
  110.  
  111.       if (match(filename, /\.[^.]+$/)) {
  112.     ref = substr(filename, 1, RSTART - 1) "+" substr(filename, RSTART + 1);
  113.       }
  114.       else {
  115.     ref = filename;
  116.       }
  117.       print "<DT> <a href=\"http:/cgi-bin/man2html?" ref "\">";
  118.       print filename;
  119.       print "</A>";    
  120.     }
  121.  
  122.     text = substr(matchline, length(filename) + 2);
  123.     tcount++;
  124.     if (tcount < truncate_at) {
  125.       sub(/^ *.[^ ]+ /, "", text);
  126.       sub(/ +$/, "", text);
  127.       gsub(/\\f./,    "", text);
  128.       gsub(/\\&/,     "", text);
  129.       gsub(/\\/,      "", text);
  130.       print "<DD>" text;
  131.     }
  132.     else if (tcount == truncate_at) {
  133.       print "<DD> <I>...additional matches not shown.</I>";
  134.     }
  135.   }
  136.  
  137.   print "</DL>";
  138.   if (mcount == 0) {
  139.     print "No matches found.";
  140.   }
  141.   else {
  142.     print "<HR>\n<P>" mcount " matches found."
  143.   }
  144.   print "</BODY>";
  145.   print "</HTML>";
  146.   exit;    
  147. }' "$@"
  148.  
  149.